import { Module } from 'vuex';
import type { ISystemState } from './types';
import type { IRootState } from '../types';
import type { IResponseData } from '@/service/system/types';
import {
  getTableData,
  deleteTableData,
  createTableData,
  editTableData
} from '@/service/system/user';
import { ElNotification } from 'element-plus';
const userModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersTableData: {
        list: [],
        totalCount: 0
      },
      roleTableData: {
        list: [],
        totalCount: 0
      },
      goodsTableData: {
        list: [],
        totalCount: 0
      },
      menuTableData: {
        list: [],
        totalCount: 0
      },
      searchInfo: {}
    };
  },

  mutations: {
    saveTableData(state, playload) {
      const { data, pageName } = playload;
      (state as any)[`${pageName}TableData`] = data;
    },
    saveSearchInfo(state, playload) {
      state.searchInfo = playload;
    }
  },
  getters: {
    pageContentList: (state) => {
      return (pageName: string) => {
        return (state as any)[`${pageName}TableData`].list;
      };
    },
    pageContentCount: (state) => {
      return (pageName: string) => {
        return (state as any)[`${pageName}TableData`].totalCount;
      };
    }
  },

  actions: {
    async getTableList({ commit }, playload: IResponseData) {
      const { pageName, queryInfo } = playload;
      const requestUrl = `/${pageName}/list`;
      const res = await getTableData(requestUrl, queryInfo);
      if (res.code === 1) {
        commit('saveTableData', { data: res.data, pageName });
      }
    },
    async deletePageData(
      { dispatch, state },
      playload: { pageName: string; id: number }
    ) {
      const { pageName, id } = playload;
      const requestUrl = `/${pageName}/${id}`;
      const res = await deleteTableData(requestUrl);
      if (res.code === 1) {
        console.log(res.msg);
        ElNotification({
          title: 'Success',
          message: res.msg,
          duration: 3000,
          type: 'success'
        });
      } else {
        ElNotification({
          title: 'Error',
          message: res.msg,
          duration: 3000,
          type: 'error'
        });
        console.log(res.msg);
      }
      dispatch('getTableList', {
        pageName,
        queryInfo: { offset: 0, size: 10, ...state.searchInfo }
      });
    },
    async createData({ state, dispatch }, playload) {
      const { pageName, data } = playload;
      const requestUrl = `/${pageName}`;
      const res = await createTableData(requestUrl, data);
      if (res.code === 1) {
        dispatch('getTableList', {
          pageName,
          queryInfo: { offset: 0, size: 10, ...state.searchInfo }
        });
      }
    },
    async editData({ state, dispatch }, playload) {
      const { pageName, data, id } = playload;
      const requestUrl = `/${pageName}/${id}`;
      const res = await editTableData(requestUrl, data);
      if (res.code === 1) {
        dispatch('getTableList', {
          pageName,
          queryInfo: { offset: 0, size: 10, ...state.searchInfo }
        });
      }
    }
  }
};

export default userModule;
